package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.PurchaseRequest;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.PurchaseRequestService;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-17 12:05
 * @name 采购申请
 */
@Controller
public class PurchaseRequestController {

    /*自动注入*/
    @Autowired
    PurchaseRequestService purchaseRequestService;

    /*查询所有采购申请*/
    @ResponseBody
    @GetMapping ("/purchaserequest")
    public ResponseResult<List<PurchaseRequest>> getPurchaseRequestAll(int page, int limit,PurchaseRequest purchaseRequest,String searchDate){


        ResponseResult<List<PurchaseRequest>> purchaseResponseResult = null;
        /*查询总条数*/
        int allCount = purchaseRequestService.selectAllCount();
        try {
            /*查询当前数据*/
            List<PurchaseRequest> purchaseRequests = purchaseRequestService.getPurchaseRequestAll(page,limit,purchaseRequest,searchDate);

            purchaseResponseResult = new ResponseResult<>(AuditType.No_Approval,"查询成功",allCount,purchaseRequests);
        }catch (RuntimeException e){
            e.printStackTrace();
            purchaseResponseResult = new ResponseResult<>();
            purchaseResponseResult.setMsg(e.getMessage());
        }
        return purchaseResponseResult;
    }

    /*插入订单*/
    @ResponseBody
    @PostMapping("/purchaserequest")
    public ResponseResult insertPurchase(String proposer, Date requesttime,String requestremark){


        ResponseResult responseResult = purchaseRequestService.insertPurchase(proposer,requesttime,requestremark);


        return responseResult;
    }
}
